package com.iplant.wms.controller.batch;

import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.controller.BasicController;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.wms.service.SMSSService;
import com.iplant.wms.service.po.batch.MSSBatchRecord;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/MSSBatchRecord")
public class MSSBatchRecordController extends BasicController<MSSBatchRecord> {

    @Autowired
    SMSSService wWMSService;

    public MSSBatchRecordController() {
        super(MSSBatchRecord.class);
    }


    //根据采购入库单  生成批次信息 todo

    //根据生产入库单  生成批次信息 todo

    //根据生产入库单  生成批次信息 todo




    /**
     * 批量生成二维码（传要打印的内容）
     *
     * @param request
     * @param wParam
     * @return
     */
    @PostMapping("/CreateQRCodeList")
    public Object CreateQRCodeList(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();

        try {
            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            if (!wParam.containsKey("data")) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            // 获取参数
            List<String> wStringList = CloneTool.CloneArray(wParam.get("data"), String.class);
            if (wStringList == null || wStringList.isEmpty() || wStringList.stream().anyMatch(p -> StringUtils.isEmpty(p))) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<List<String>> wServiceResult = wWMSService.WMS_CreateQRCodeList(wLoginUser, wStringList);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            log.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }

        return wResult;
    }
}
